package com.skyline.manage.feed.task;

import org.apache.log4j.Logger;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.skyline.manage.feed.ConfigLoader;

/**
 * 
 * @author jairus
 * 
 */
public class FeedMoveTaskElement {

	private Logger logger = Logger.getLogger(FeedMoveTaskElement.class);

	private DBCollection MOVE_FROM_COLLECTION;

	private DBCollection MOVE_TO_COLLECTION;

	private Integer LIFE;

	private String TIME_COLUMN;

	private String MOVE_FROM_COLLECTION_NAME;

	private String MOVE_TO_COLLECTION_NAME;

	public FeedMoveTaskElement(DBCollection from, DBCollection to, Integer life) {
		this.MOVE_FROM_COLLECTION = from;
		this.MOVE_TO_COLLECTION = to;
		this.LIFE = life;
		this.MOVE_FROM_COLLECTION_NAME = from.getFullName();
		this.MOVE_TO_COLLECTION_NAME = to.getFullName();
		this.TIME_COLUMN = ConfigLoader.getValue("feed.column.time");
	}

	public void run() {
		Long time = System.currentTimeMillis() - this.LIFE;
		DBObject query = new BasicDBObject();
		DBObject timeRequiration = new BasicDBObject("$lt", time);
		query.put(TIME_COLUMN, timeRequiration);
		DBCursor cursor = MOVE_FROM_COLLECTION.find(query);
		int total = 0;
		while (cursor.hasNext()) {
			DBObject next=cursor.next();
			MOVE_FROM_COLLECTION.remove(next);
			MOVE_TO_COLLECTION.insert(next);
			total++;
		}
		logger.info(this.MOVE_FROM_COLLECTION_NAME + "向" + this.MOVE_TO_COLLECTION_NAME + "迁移" + total + "条记录");
	}
}
